# -*- coding:utf-8 -*-
import redis

__all__ = ['RedisBase']


class RedisBase:
    def __init__(self):
        self.conn = redis.Redis(host='127.0.0.1', port=56379, decode_responses=True)
        self.chan_sub = None
        self.chan_pub = None

    def public(self, msg):
        if self.chan_pub:
            self.conn.publish(self.chan_pub, msg)  # 开始发布消息
            return True
        else:
            raise ValueError('chan_sub dose not Initialize')

    def subscribe(self):
        if self.chan_sub:
            pub = self.conn.pubsub()  # 开始订阅
            pub.subscribe(self.chan_sub)  # 订阅频道
            pub.parse_response()  # 准备接收
            return pub
        else:
            raise ValueError('chan_sub dose not Initialize')